package com.ruoyi.redtea.dualprevent.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.redtea.dualprevent.domain.bo.TrainRiskPointControlWaysBo;
import com.ruoyi.redtea.dualprevent.domain.entity.TrainRiskPointControlWays;
import com.ruoyi.redtea.dualprevent.domain.entity.TrainRiskPointControlWaysAvatar;
import com.ruoyi.redtea.dualprevent.mapper.TrainRiskPointControlWaysAvatarMapper;
import com.ruoyi.redtea.dualprevent.mapper.TrainRiskPointControlWaysMapper;
import com.ruoyi.redtea.dualprevent.service.TrainRiskPointControlWaysService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lgp
 * @description 针对表【train_risk_point_control_ways(风险点管控措施)】的数据库操作Service实现
 * @createDate 2024-05-15 13:09:44
 */
@Service
public class TrainRiskPointControlWaysServiceImpl implements TrainRiskPointControlWaysService {

    @Resource
    private TrainRiskPointControlWaysMapper waysMapper;
    @Resource
    private TrainRiskPointControlWaysAvatarMapper avatarMapper;

    @Override
    public List<TrainRiskPointControlWays> queryList() {
        return waysMapper.selectList();
    }

    @Override
    public List<TrainRiskPointControlWaysAvatar> queryAvatarList() {
        return avatarMapper.selectList();
    }

    /**
     * 插入措施与排查项  由PC前端操作得出排查项不可单独插入或更新
     * @param controlWaysBo
     * @return
     */
    @Override
    public boolean insertRiskPointControlWaysAndItem(TrainRiskPointControlWaysBo controlWaysBo) {
        //措施表  批量插入排查项表
        TrainRiskPointControlWays pointControlWays = BeanUtil.toBean(controlWaysBo, TrainRiskPointControlWays.class);
        waysMapper.setDelFlag(pointControlWays);
        boolean insert = waysMapper.insert(pointControlWays) == 1;
        boolean itemsInsert = insertOrUpdateBatchControlWayItem(pointControlWays.getId(), controlWaysBo.getWaysItems());
        if(insert&&itemsInsert){
            return true;
        }else {
            throw new ServiceException("添加措施失败");
        }
    }

    @Override
    public boolean updateRiskPointControlWaysAndItem(TrainRiskPointControlWaysBo controlWaysBo) {
        TrainRiskPointControlWays pointControlWays = BeanUtil.toBean(controlWaysBo, TrainRiskPointControlWays.class);
//        waysMapper.updateBatchById(pointControlWays);
        boolean insert = waysMapper.updateById(pointControlWays) == 1;
        boolean itemsInsert = insertOrUpdateBatchControlWayItem(pointControlWays.getId(), controlWaysBo.getWaysItems());
        if(insert&&itemsInsert){
            return true;
        }else {
            throw new ServiceException("添加措施失败");
        }
    }


    /**
     * 插入或更新排查项
     * @param pointId
     * @param avatarList
     * @return
     */
    @Override
    public boolean insertOrUpdateBatchControlWayItem(Integer pointId, List<TrainRiskPointControlWaysBo.WaysItem> avatarList) {
        List<TrainRiskPointControlWaysAvatar> items = new ArrayList<>();
        for (TrainRiskPointControlWaysBo.WaysItem wayItem : avatarList) {
            TrainRiskPointControlWaysAvatar avatar = new TrainRiskPointControlWaysAvatar();
            if(wayItem.getId()!=null){
                avatar.setId(wayItem.getId());
            }
            avatar.setWaysId(pointId);
            avatar.setCheckBasis(wayItem.getCheckBasis());
            avatar.setContentCheck(wayItem.getContentCheck());
            avatar.setTroubleLevel(wayItem.getTroubleLevel());
            items.add(avatar);
        }
        avatarMapper.setListDelFlag(items);
        return avatarMapper.insertOrUpdateBatch(items);
    }


    /**
     * 删除排查项，插入或更新时跟随措施一起实施，删除由前端单独调用，达到编辑时灵活增减
     * @param itemId
     * @return
     */
    @Override
    public boolean delControlWayItem(Integer itemId) {//措施里列表项单个增减时调用
        return avatarMapper.deleteById(itemId) == 1;
    }

}
